﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NXDO.WebAPI.Controllers;
using NXDO.WebAPI.Controllers.Info;
using System;
using System.Collections.Generic;
using TDM.DbData.DataEntity;

namespace TDM.WebSvr.Controllers.Project
{
    /// <summary>
    /// 试验任务接口
    /// </summary>
    [AllowAnonymous]
    public class TestTaskController : ApiDbEntityController<TestTask, long>
    {
        protected override string GetPkName()
        {
            return "TaskID";
        }

        protected override string GetTableName()
        {
            return "TestTask";
        }

        public override TestTask Post([FromBody] TestTask data)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            data.CreateTime = DateTime.Now;
            data.CreateUID = uid;
            return base.Post(data);
        }

        public override List<TestTask> PostCollection([FromBody] List<TestTask> datas)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            datas.ForEach(data =>
            {
                data.CreateTime = DateTime.Now;
                data.CreateUID = uid;
            });
            return base.PostCollection(datas);
        }

        public override bool Delete(long pkValue)
        {
            return WebBiz.Project.TestTaskHelper.DeleteTestTask(pkValue);
        }

        public override List<bool> DeleteCollection([FromBody] List<long> pkValues)
        {
            return WebBiz.Project.TestTaskHelper.DeleteTestTask(pkValues);
        }

        ///<inheritdoc/>
        public override ApiPagerResult<TestTask, long> DoPager([FromBody] ApiPagerInfo<long> info)
        {
            return base.DoPager(info);
        }

        /// <summary>
        /// 分页查询我的任务
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [HttpPost]
        public ApiPagerResult<TestTask, long> QueryMyTask([FromBody] ApiPagerInfo<long> info)
        {
            var user = HttpContext.User as NXDO.Data.Security.Principal.MemberPrincipal;
            long uid = Convert.ToInt64(user?.MemberId);
            if (uid == 0) throw new Exception("未获得登录信息");
            info.Where += $" and createUID = {uid}";
            return base.DoPager(info);
        }

        /// <summary>
        /// 查询项目下的试验任务列表(不含已删除)
        /// </summary>
        /// <param name="prjid"></param>
        /// <returns></returns>
        [HttpGet]
        public List<TestTask> QueryPrjTasks(long prjid)
        {
            return WebBiz.Project.TestTaskHelper.QueryPrjTasks(prjid);
        }
        /// <summary>
        /// 查询项目下的试验任务列表(包含已删除)
        /// </summary>
        /// <param name="prjid"></param>
        /// <returns></returns>
        [HttpGet]
        public List<TestTask> QueryPrjTasksIncludeDelete(long prjid)
        {
            return WebBiz.Project.TestTaskHelper.QueryPrjTasksIncludeDelete(prjid);
        }
    }
}
